<template>
  <vue-content>
    <div class="left">
      <el-button type="primary" @click="goall">全部栏目</el-button>
      <el-tree 
        :data="tree" 
        @node-click="handleNodeClick"
      >
      </el-tree>
    </div>
    <vue-header  v-if="vueContent">
      <vue-header-button>
        <el-button type="primary" @click="handleCommand">新增</el-button>
      </vue-header-button>
    </vue-header>
    <vue-body  v-if="vueContent">
      <el-table
        v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="栏目id">
        </el-table-column>
          <el-table-column
          prop="columnName"
          label="栏目名称">
        </el-table-column>
          <el-table-column
          prop="channelPath"
          label="栏目路径">
        </el-table-column>
        <el-table-column
          prop="priority"
          label="排列顺序">
        </el-table-column>
        <el-table-column
          prop="isDisplay"
          label="是否显示">
          <template scope="scope">
            {{ scope.row.isDisplay  === '1'? '是': '否' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button
              size="small"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index, scope.row, tableData)">删除</el-button>
          </template>
      </el-table-column>
      </el-table>
    </vue-body>
    <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;"  v-if="vueContent">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='create' || view ==='edit'"/>
  </vue-content>
</template>

<script>
  import {queryPageColumn,editStatusColumn,queryMenuColumn} from '@/services/program'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  export default {
    name: 'role-table',
    components: {EditRole},
    data: () => ({
      vueContent:true,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
        id: '',
        columnName:"",
        parentId:""
      },
      tree:[],
      loading: false,
      view: '',
    }),
    methods: {
      async handleCommand(command) {
        this.view = 'create'
        await this.$nextTick()
        this.$refs['edit-role'].dialogFormVisible = true;
        this.$refs['edit-role'].parentdata = deepClone(this.tree);
        this.vueContent = false;
      },
      async handleNodeClick(data,Node) {
      	this.loadData(data.id);
      },
      async queryMenu(){
            let data = await queryMenuColumn()
            this.tree = data.rootTrees;
            let  tableData = data.columnList;
      },
      goall(){
        this.loadData()
      },
      async loadData (id) {
        this.loading = true;
        try {
          let data = await queryPageColumn(Object.assign({currentPage: this.currentPage},{perPage:10},{parentId:id}))
          // debugger
          this.tableData = data.columnList;
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async handleEdit (index, data) {
        this.view = 'edit'
        await this.$nextTick()
        this.$refs['edit-role'].dialogFormVisible = true
        this.$refs['edit-role'].formData = deepClone(data)
        this.$refs['edit-role'].parentdata = deepClone(this.tree);
        this.$refs['edit-role'].imgShow = 'edit'
        this.vueContent = false;
      },
      handleDelete (index, data, rows) {
        this.$confirm(`此操作将删除${data.columnName}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          try {
            await editStatusColumn({
              id:data.id,isDisplay:"2"
            })
            this.$message({
              type: 'success',
              message: '已删除'
            })
            rows.splice(index, 1)
          } catch (e) {
            if (e instanceof RequestError) {
              this.$alert(e.message, '提示', {
                type: 'error',
                confirmButtonText: '确定'
              })
            } else {
              throw e
            }
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      SearchSubmit () {
        this.currentPage = 1
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      editFinish (needRefresh) {
        this.view = ''
        this.vueContent = true;
        if (needRefresh) {
          this.loadData()
        }
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData()
      }
    },
    filters: {
      type (value) {
        return {0: '管理员', 1: '操作员'}[value]
      }
    },
    created () {
      this.loadData();
      this.queryMenu();
    }
  }
</script>
<style scoped>
.vue-content{
    padding-left: 150px;
    position: relative;
}
.el-tree {
   
   border: none;
    
}
.left{
  position: absolute;
   width: 135px;
   left: 0px;
   top: 25px;
   height:400px;
   overflow: auto;
}
</style>




